﻿#define _CRT_SECURE_NO_WARNINGS 1
#include"snake.h"
void set_pos(short x, short y)
{
	//获取标准输出设备的句柄
	HANDLE houtput = NULL;
	houtput = GetStdHandle(STD_OUTPUT_HANDLE);

	//定位到光标的位置
	COORD pos = { x,y };
	SetConsoleCursorPosition(houtput, pos);
}
void WelconmeToGame()
{
	set_pos(40, 14);
	wprintf(L"欢迎来到贪吃蛇小游戏\n");
	set_pos(42, 20);
	system("pause");
	system("cls");//清理屏幕
	set_pos(25, 14);
	wprintf(L"⽤ ↑ . ↓ . ← . → 分别控制蛇的移动， F3为加速，F4为减速\n");
	set_pos(42, 15);
	wprintf(L"加速可以得到更多的分数\n");
	set_pos(42, 20);
	system("pause");
	system("cls");
}

void GameStart(pSnake ps)
{
	//0.先设置窗口大小，再光标隐藏
	system("mode con cols=100 lines=30");
	system("title 贪吃蛇");

	HANDLE houtput = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(houtput, &CursorInfo);//获取控制台光标信息
	CursorInfo.bVisible = false;//隐藏控制台光标
	SetConsoleCursorInfo(houtput, &CursorInfo);
	
	//1.打印环境界面
	//功能介绍
	WelconmeToGame();
	//2.绘制地图
	CreatMap();
	//3.创建蛇
	InitSnake(ps);
	//4.创建食物
	CreateFood(ps);

	//游戏运行
	GameRun(ps);
	//getchar();
}
void CreatMap()
{
	//上
	int i = 0;
	for (i = 0; i < 29;i++)
	{
		wprintf(L"%c", WALL);
	}

	//下
	set_pos(0, 26);
	for (i = 0;i < 29; i++)
	{
		wprintf(L"%c", WALL);
	}

	//左
	for (i = 1; i <= 25; i++)
	{
		set_pos(0, i);
		wprintf(L"%c", WALL);
	}
	//getchar();

	//右
	for (i = 1; i <= 25; i++)
	{
		set_pos(56, i);
		wprintf(L"%c", WALL);
	}
	//getchar();
}
void CreateFood(pSnake ps)
{
	int x = 0; int y = 0;

	//生成x是2的倍数
	//x：2-54
	//y:1-25

	again:
	do
	{
		x = rand() % 53 + 2;
		y = rand() % 25 + 1;
	} while (x % 2 != 0);
	//x,y不能和蛇身体坐标冲突
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		if (x == cur->x && y == cur->y)
		{
			goto again;
		}
			cur = cur->next;
	}
	//创建食物节点
	pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pFood == NULL)
	{
		perror("CreateFood()::malloc()");
		return;
	}
	else
	{
		pFood->x = x;
		pFood->y = y;
		pFood->next = NULL;

		set_pos(x, y);
		wprintf(L"%c", FOOD);
		ps->_pFood = pFood;
	}
}
void InitSnake(pSnake ps)
{
	int i = 0;
	pSnakeNode cur = NULL;
	for (i = 0; i < 5; i++)
	{
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("InisSnake()::malloc()");
			return;
		}
		cur->next = NULL;
		cur->x = POS_X + 2 * i;
		cur->y = POS_Y;


		//头插法插入链表
		if (ps->_pSnake == NULL)//空链表
		{
			ps->_pSnake = cur;
		}
		else//非空链表
		{
			cur->next = ps->_pSnake;
			ps->_pSnake = cur;
		}
		cur = ps->_pSnake;
		
	}
	while (cur)
	{
		set_pos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}

	//设置贪吃蛇的属性
	ps->_dir = RIGHT;//默认向右
	ps->_soure = 0;
	ps->_food_weight = 10;
	ps->_sleep_time = 200;//移动一次休息200毫秒
	ps->_status = OK;

	//getchar();
}


void Pause()
{
	while (1)
	{
		Sleep(200);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}
}
bool NextIsFood(pSnakeNode pn, pSnake ps)
{
	if (ps->_pFood->x == pn->x && ps->_pFood->y == pn->y)
	{
		return true;
	}
	else
	{
		return false;
	}
}
void EatFood(pSnakeNode pn, pSnake ps)
{
	//头插法
	ps->_pFood->next = ps->_pSnake;
	ps->_pSnake = ps->_pFood;

	//释放下一个位置的结点
	free(pn);
	pn = NULL;
	pSnakeNode cur = ps->_pSnake;

	//打印
	while (cur)
	{
		set_pos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	ps->_soure += ps->_food_weight;

	//重新创建食物
	CreateFood(ps);
}
void NoFood(pSnakeNode pn, pSnake ps)
{
	pn->next = ps->_pSnake;
	ps->_pSnake = pn;

	pSnakeNode cur = ps->_pSnake;
	while (cur->next->next != NULL)
	{
		set_pos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}

	//把最后一个结点打印成空格
	set_pos(cur->next->x, cur->next->y);
	printf("  ");

	//释放最后一个结点
	free(cur->next);

	//把倒数第二个结点的next为空
	cur->next = NULL;
}
void killByWall(pSnake ps)
{
	if (ps->_pSnake->x == 0 || ps->_pSnake->x == 56 ||
		ps->_pSnake->y == 0 || ps->_pSnake->y == 56)
	{
		ps->_status = KILL_BY_WALL;

	}

}
void killByself(pSnake ps)
{
	pSnakeNode cur = ps->_pSnake->next;
	while (cur)
	{
		if (cur->x == ps->_pSnake->x && cur->y == ps->_pSnake->y)
		{
			ps->_status = KILL_BY_SELF;
			break;
		}
		cur = cur->next;
	}
}
void SnakeMove(pSnake ps)
{
	//getchar();
	//创建一个结点，表示蛇即将到的下一个结点
	pSnakeNode pNextNode = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pNextNode == NULL)
	{
		perror("SnakeMove()::malloc()");
		return;
	}
	switch (ps->_dir)
	{
	case UP:
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y = ps->_pSnake->y - 1;

		break;
	case DOWN:
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y = ps->_pSnake->y + 1;
		break;
	case LEFT:
		pNextNode->x = ps->_pSnake->x - 2;
		pNextNode->y = ps->_pSnake->y;
		break;
	case RIGHT:
		pNextNode->x = ps->_pSnake->x + 2;
		pNextNode->y = ps->_pSnake->y;
		break;
	}
	//检测下一个坐标处是否是食物
	if (NextIsFood(pNextNode,ps))
	{
		EatFood(pNextNode, ps);
	}
	else
	{
		NoFood(pNextNode, ps);
	}


	//检测是否撞墙
	killByWall(ps);
	//检测是否撞到自己
	killByself(ps);
}
void PrintHelpInfo()
{
	set_pos(62, 10);
	wprintf(L"%ls", L"不能穿墙，不能咬到自己");
	set_pos(62, 11);
	wprintf(L"%ls", L"用 ↑ . ↓ . ← . → 分别控制蛇的移动");
	set_pos(62, 12);
	wprintf(L"%ls", L"按F3加速，F4减速");
	set_pos(62, 13);
	wprintf(L"%ls", L"按ESC退出游戏，按空格暂停游戏");
	
}

void GameRun(pSnake ps)
{
	//打印帮助信息
	PrintHelpInfo();
	do
	{
		//打印总分数和食物的分值
		set_pos(62, 9);
		printf("总分数:%d ", ps->_soure);
		//set_pos(62, 10);
		printf("当前单个食物的分数:%2d\n", ps->_food_weight);
		if (KEY_PRESS(VK_UP) && ps->_dir != DOWN)
		{
			ps->_dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->_dir != UP)
		{
			ps->_dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_dir != RIGHT)
		{
			ps->_dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_dir != LEFT)
		{
			ps->_dir = RIGHT;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			//暂停
			Pause();
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			//正常退出游戏
			ps->_status = END_NORMAL;
		}
		else if (KEY_PRESS(VK_F3))
		{
			//加速 最多加四档
			if(ps->_sleep_time>80)
			{
				ps->_sleep_time -= 30;
				ps->_food_weight += 2;
			}

		}
		else if (KEY_PRESS(VK_F4))
		{
			//减速
			if(ps->_food_weight>2)
			{
				ps->_sleep_time += 30;
				ps->_food_weight -= 2;
			}
		}
		SnakeMove(ps);//蛇走一步的过程
		Sleep(ps->_sleep_time);
	} while (ps->_status == OK);
	
}
void GameEnd(pSnake ps)
{
	set_pos(24, 12);
	switch (ps->_status)
	{
		
	case END_NORMAL:
		printf("已主动结束游戏\n");
		break;
	case KILL_BY_WALL:
		printf("您撞到了墙上，游戏结束\n");
		break;
	case KILL_BY_SELF:
		printf("您撞到了自己，游戏结束");
		break;
	}

	//释放蛇身的链表
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		pSnakeNode del = cur;
		cur = cur->next;
		free(del);
	}
	
}
